package org.petstore.web.servlet;

import org.petstore.domain.Cart;
import org.petstore.domain.CartItem;
import org.petstore.domain.Item;
import org.petstore.domain.Account;
import org.petstore.service.CartService;
import org.petstore.service.LogService;
import org.petstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AddItemToCartServlet extends HttpServlet {
    //Servlet的功能即负责中转
    //1.处理完请求后的跳转页面
    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";

    //2.定义处理该请求所需要的数据
    private String workingItemId;
    private List<CartItem> cart;

    private CartService cartService;
    //3.是否需要调用业务逻辑层
    private CatalogService catalogService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        workingItemId = request.getParameter("workingItemId");
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute("account");
        if(account==null)
        {
            request.getRequestDispatcher(SIGNON).forward(request, response);
        }
        else
        {
            String userId = account.getUsername();
            cartService = new CartService();
            CartItem cartItem = cartService.getCartItemByUsernameAndItemId(userId, workingItemId);
            //购物车里有该品种
            if (cartItem != null) {
                if(!cartItem.isPay()) {
                    //若显示未付款，则数量加1
                    cartService.incrementItemByUsernameAndItemId(userId, workingItemId);
                    if(account != null){
                        HttpServletRequest httpRequest= (HttpServletRequest) request;
                        String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                                + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                        LogService logService = new LogService();
                        //Item item = catalogService.getItem(workingItemId);
                        String logInfo = logService.logInfo(" ") + strBackUrl +"数量+1 ";
                        logService.insertLogInfo(account.getUsername(), logInfo);
                    }
                }
                else {
                    //若已付款，数量为1
                    cartService.updateItemByItemIdAndPay(userId, workingItemId, false);
                    cartService.updateItemByItemIdAndQuantity(userId, workingItemId, 1);
                    if(account != null){
                        HttpServletRequest httpRequest= request;
                        String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                                + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                        LogService logService = new LogService();
                        String logInfo = logService.logInfo(" ") + strBackUrl + " 添加物品到购物车";
                        logService.insertLogInfo(account.getUsername(), logInfo);
                    }
                }
            }
            else{//无该品种
                catalogService = new CatalogService();
                boolean isInStock = catalogService.isItemInStock(workingItemId);
                Item item = catalogService.getItem(workingItemId);
                cartService.addItemByUsernameAndItemId(userId, item, isInStock);
            }
            //List<CartItem> cart = (List<CartItem>) session.getAttribute("cart");
            cart = cartService.selectItemByUsername(userId);
            session.setAttribute("cart", cart);
            request.getRequestDispatcher(VIEW_CART).forward(request, response);
        }
    }
}
